jQuery(function($) {
	var doc = document,
		doc_ = $(doc); //将document对像缓存以保证性能
		
	doc_.on("click", ".head li a", function(event) {
		var link = $(this),
			links = $(".head li a");
			
		$.each(links, function(i, item) {
			links.eq(i).removeClass("active");
		});
		link.addClass("active");
	});
	
	//兼容安桌和IOS
	
	
	//以下两个事件为显示表单
		//ios事件处理
	doc_.on("touchstart", ".nav_list .list", function(event) {
		event.preventDefault();	//阻止冒泡到click事件
		$(".nav_list .search_info").addClass("test");
		$(".nav_list .search_info input").addClass("stretching");
		
		$(".stretching").focus();
	});
		
	doc_.on("click", ".nav_list .list", function(event) {
		$(".nav_list .search_info").addClass("test");
		$(".nav_list .search_info input").addClass("stretching");
		
		$(".stretching").focus();
	});
	
		//ios事件处理
	doc_.on("touchstart", ".nav_list .proList", function(event) {
		event.preventDefault();	//阻止冒泡到click事件
		$(".nav_list .search_info input").addClass("stretching");
		
		$(".stretching").focus();
	});
	
	doc_.on("click", ".nav_list .proList", function(event) {
		$(".nav_list .search_info input").addClass("stretching");
		
		$(".stretching").focus();
	});
	
	//当不符合表单出现条件则关闭表单
	doc_.on("touchstart", function(event) {
		
		var target = event.target;
		if (target.className !== "list" && target.className !== "proList" &&
			target.tagName !== "INPUT") {
			$(".nav_list .search_info input").removeClass("stretching");
		}
		//event.preventDefault();
	});
	
	doc_.on("click", function(event) {
		var target = event.target;
		if (target.className !== "list" && target.className !== "proList" &&
			target.tagName !== "INPUT" ) {
			$(".nav_list .search_info input").removeClass("stretching");
		}
	});
	
	//删除class=proList的列表按钮(单独删除)
	doc_.on("touchstart", ".proList .close", function(event) {
		//console.log();
		var elm = $(this).parent();
		elm.addClass("fadeout");
		setTimeout(function() {
			elm.remove();
		}, 500);
	});
	
	doc_.on("click", ".proList .close", function(event) {
		//console.log();
		var elm = $(this).parent();
		elm.addClass("fadeout");
		setTimeout(function() {
			elm.remove();
		}, 500);
	});
	
	//删除class=proList的列表按钮(全部删除)
	doc_.on("touchstart", ".proList .clear", function(event) {
		var elms = $(".proList p");
		$.each(elms, function(i, item) {
			elms.eq(i).addClass("fadeout");
		});
		
		//0.5秒后移除元素
		setTimeout(function() {
			$.each(elms, function(i, item) {
				elms.eq(i).remove();
			});
		}, 500);
	});
	
	doc_.on("click", ".proList .clear", function(event) {
		var elms = $(".proList p");
		$.each(elms, function(i, item) {
			elms.eq(i).addClass("fadeout");
		});
		
		//0.5秒后移除元素
		setTimeout(function() {
			$.each(elms, function(i, item) {
				elms.eq(i).remove();
			});
		}, 500);
	});
	
	//获取class="stretching"表单值，并将其加入到class="proList"中
	doc_.on("keyup", function(event) {
		
		var elm = $(".stretching"),
			val = elm.val();
		console.log(val === undefined);
		if (event.which === 13 && val !== undefined) {
			var proList = $(".proList");
			 proList.append("<p>" + val + "<em class='close'></em></p>");
			 elm.val("");
		}
	});
	
	//将filter-row值添加到class=proList中
	doc_.on("click", ".filter-row a", function(event) {
		var target = event.target;
		if (target.tagName === "A") {
			var string = target.innerHTML;
			
			//用于判断filter值是否已加入到proList中，如果未加入则index = 1
			//否则不将值加入到proList中
			if (target.index !== 1) {

				target.index = 1;
				var text = target.innerHTML;
				var proList = $(".proList");
				proList.append("<p>" + text + "<em class='close'></em></p>");
				return;
			}

		}
		
	});
	
	//如果删除了filter-row中的值,则将index恢得到初始状态
	doc_.on("click", ".proList .close", function(event) {
		console.log();
		if (event.target.parentNode.index === 1) {
			event.target.parentNode.index = 0;
		}
		
	});
	
	//切换filter-row的值
	function setIndex() {
		var links = doc.querySelectorAll(".row nav a"),
			len = links.length;
		
		for (var i = 0;i < len; i++) {
			links[i].index = i;
		}
		//console.log(links[2].index);
	}
	setIndex();
	
	//切换tab标签
	doc_.on("click", ".row a", function(event) {
		var rows = $(".row ul"),
			rowelm = $(".filter-row"),
			links = $(".row nav a"),
			tabs = $(".filter-row ul"),
			index = event.target.index;
			
		$.each(tabs, function(i, item) {
			tabs.eq(i).addClass("hide");
			tabs.eq(i).removeClass("show");
			links.eq(i).removeClass("selected");
		});
		
		rowelm.addClass("show");
		tabs.eq(index).addClass("show");
		links.eq(index).addClass("selected");
		//console.log(links.length);
	});
	
});